package com.pgeg.registry;

import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
import net.fabricmc.fabric.api.biome.v1.BiomeSelectors;
import net.fabricmc.fabric.api.biome.v1.OverworldBiomes;
import net.fabricmc.fabric.api.biome.v1.OverworldClimate;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.BuiltinRegistries;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.GenerationStep;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.surfacebuilder.ConfiguredSurfaceBuilder;
import net.minecraft.world.gen.surfacebuilder.TernarySurfaceConfig;

import static com.pgeg.gen.MagicOakForestBiomeFeature.MAGIC_OAK_FOREST;
import static com.pgeg.gen.MagicOakForestBiomeFeature.MAGIC_OAK_FOREST_SURFACE_BUILDER;
import static com.pgeg.gen.ModOreFeature.*;
import static com.pgeg.listener.WorldGenListener.*;
import static com.pgeg.util.Reference.MODID;

public class WorldGenRegistry {

    public static void registerWorldGen() {

        /*Ores World Gen*/
        initWorldGen(oreCopperOverworld, ORE_COPPER_OVERWORLD);
        initWorldGen(ore_mana_crystal_overworld, MANA_CRYSTAL_OVERWORLD);
        initWorldGen(oreOrichalcumOverworld, ORICHALCUM_OVERWORLD);
        initWorldGen(oreSaturniumOverworld, SATURNIUM_OVERWORLD);
        initWorldGen(oreBrimstoneOverworld, BRIMSTONE_OVERWORLD);
        initWorldGen(oreMeteoriteOverworld, METEORITE_OVERWORLD);
        initWorldGen(oreSilverOverworld, ORE_SILVER_OVERWORLD);
        initWorldGen(oreMithrilOverworld, MITHRIL_OVERWORLD);
        initWorldGen(oreFireInfusedStoneOverworld, FIRE_INFUSED_STONE_OVERWORLD);
        initWorldGen(oreEarthInfusedStoneOverworld, EARTH_INFUSED_STONE_OVERWORLD);
        initWorldGen(oreWaterInfusedStoneOverworld, WATER_INFUSED_STONE_OVERWORLD);
        initWorldGen(oreOrderInfusedStoneOverworld, ORDER_INFUSED_STONE_OVERWORLD);
        initWorldGen(oreEntropyInfusedStoneOverworld, ENTROPY_INFUSED_STONE);

        /*Biome World Gen*/
        initWorldGen("magic_oak_forest", MAGIC_OAK_FOREST_SURFACE_BUILDER, MAGIC_OAK_FOREST_KEY, MAGIC_OAK_FOREST, OverworldClimate.TEMPERATE, 1.2D);
    }

    private static void initWorldGen(RegistryKey<ConfiguredFeature<?, ?>> valueKey, ConfiguredFeature<?, ?> feature) {
        Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, valueKey.getValue(), feature);
        BiomeModifications.addFeature(BiomeSelectors.foundInOverworld(), GenerationStep.Feature.UNDERGROUND_ORES, valueKey);
    }

    private static void initWorldGen(String path, ConfiguredSurfaceBuilder<TernarySurfaceConfig> surface, RegistryKey<Biome> key, Biome biome, OverworldClimate type, double weight) {
        Registry.register(BuiltinRegistries.CONFIGURED_SURFACE_BUILDER, new Identifier(MODID, path), surface);
        Registry.register(BuiltinRegistries.BIOME, key.getValue(), biome);
        OverworldBiomes.addContinentalBiome(key, type, weight);
    }
}
